package griffin.website;


import android.app.Activity;
//import android.view.ScaleGestureDetector;
import android.view.MotionEvent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
//import android.view.animation.Animation;
//import android.view.animation.AnimationUtils;
import android.widget.Button;
//import android.widget.ImageView;
//import android.widget.TextView;
import android.widget.EditText;
import android.content.Context;
//import android.content.SharedPreferences;
import android.widget.CheckBox;
import android.widget.Toast;
//import android.content.SharedPreferences;
import android.content.Intent;
import java.util.concurrent.atomic.AtomicReference;
//import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicBoolean;
import android.view.WindowManager;
import android.content.pm.ActivityInfo;
import android.view.inputmethod.InputMethodManager;


import griffin.logic.userLogic;
//import griffin.website.TopHome.TouchItemClickListener;

public class Login extends Activity {
	private EditText username;
	private EditText password;
//	private TextView lblError;
	private CheckBox chkRememberLogin;
	private Button btnLogin;
	private Button btnNoLogin;
	private Button btnRegister;
	userLogic userlogic = new userLogic(this);
	
	private static final String TAG = "[login]";
    /** Called when the activity is first created. 
     * 2011*/
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	Log.d(TAG, "[onCreate...] ");	
    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        
      
        Log.d(TAG, "[onCreate...1] ");	
        username=(EditText)findViewById(R.id.username);
        password=(EditText)findViewById(R.id.password);
        chkRememberLogin=(CheckBox)findViewById(R.id.chkRememberLogin);
        btnLogin=(Button)findViewById(R.id.btnLogin);
        btnNoLogin=(Button)findViewById(R.id.btnNoLogin);
        btnRegister=(Button)findViewById(R.id.btnRegister);
//        entire_view_login
        View view = (View)findViewById(R.id.entire_view_login);
        view.setOnTouchListener(new TouchItemClickListener());
        
        Log.d(TAG, "[onCreate...2] ");	
        getLogin();
        
        Log.d(TAG, "[onCreate...3] ");	
        btnLogin.setOnClickListener(new OnClickListener(){
	 		public void onClick(View v){
	 			if(username.getText().toString().length()==0){	 				
	 				Toast.makeText(Login.this, "Please enter username!", Toast.LENGTH_SHORT).show();
	 				return;
	 			}
	 			if(password.getText().toString().length()==0){
	 				Toast.makeText(Login.this, "Please enter password!", Toast.LENGTH_SHORT).show();
	 				return;
	 			}
	 			goLogin();
	 		}
	 	});
        btnNoLogin.setOnClickListener(new OnClickListener(){
	 		public void onClick(View v){
				goBack();
	 		}
	 	});
        btnRegister.setOnClickListener(new OnClickListener(){
	 		public void onClick(View v){
				goRegister();
	 		}
	 	});
        Log.d(TAG, "[onCreate...4] ");	
    }
    
    class TouchItemClickListener implements OnTouchListener
    {
		public boolean onTouch(View v, MotionEvent event) {
			Log.d(TAG, "[onTouch...] ");	
			username.clearFocus();
			getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);			
        	InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        	imm.hideSoftInputFromWindow(username.getWindowToken(), 0);
        	
			return true;
		}		
    }
    
////    onTouchEvent(MotionEvent event)
////    ScaleGestureDetector scaleGestureDetector = new 
//    public boolean onTouchEvent(MotionEvent event) {
////    	  boolean retval = scaleGestureDetector.onTouchEvent(event);
//    	super.onTouchEvent(event);
////    	   Log.v("MyView.onTouchEvent()", "Action: " + event.getAction() +
////    	         "; PointerCount: " + event.getPointerCount() +
////    	         "; scaleGestureDetector.onTouchEvent() RetVal: " + retval);
////    	   return(retval);
//    	return false;
//	}
    
    public void getLogin(){
    	AtomicReference<String> strUsername=new AtomicReference<String>("");
    	AtomicReference<String> strPassword=new AtomicReference<String>("");
    	AtomicBoolean isRemember=new AtomicBoolean(false);
    	if(userlogic.getLoginUser(strUsername, strPassword, isRemember)){
    		if(isRemember.get()){
    			chkRememberLogin.setChecked(true);
    			username.setText(strUsername.get());
    			password.setText(strPassword.get());
    		}
    	}    
    	
    	//show keyboard
    	username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    	    public void onFocusChange(View v, boolean hasFocus) {
    	        if (hasFocus) {
//    	        	getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    	            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    	            Log.d(TAG, "[onFocusChange...GET] ");	
    	        }else{
//    	        	getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
    	        	getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    	        	
    	        	Log.d(TAG, "[onFocusChange...OFF] ");	
    	        }    	        	
    	    }
    	});
    	username.requestFocus();
    	Log.d(TAG,"set focus");
    }
    
    public void goLogin(){    	
    	try{
			Log.d(TAG, "[Login...] ");	     
			String strUsername = username.getText().toString();
			String strPassword = password.getText().toString();
			boolean isRemeber = chkRememberLogin.isChecked();
			AtomicReference<String> errorMessage = new AtomicReference<String>("");
			int userid = userlogic.checkLoginUser(strUsername, strPassword, errorMessage);
			if(userid>0){
				userlogic.setLoginUser(userid, strUsername, strPassword, isRemeber);	
					
		    	Log.d(TAG, "[Go Login] ");	
		    	openIntenInView("TopHome",Login.this);	
			}else{
				//lblError.setText(errorMessage.get());
				Toast.makeText(Login.this, "Please enter correct username or password!", Toast.LENGTH_SHORT).show();
			}
		}catch(Exception ex){
			//lblError.setText("login failed:" +ex.toString());
			Toast.makeText(Login.this, "Login failed!", Toast.LENGTH_SHORT).show();
		}
    }

    private void goBack(){
    	openIntenInView("TopHome",Login.this);
    }
    private void goRegister(){
    	openIntenInView("register",Login.this);
    }
    
//  openIntenInView("register",more.this);
    public void openIntenInView(String name,Context context){
		Intent intent = new Intent();
		intent.putExtra("winname", name); 
		intent.setClass(context, TabBarFramework.class);
		startActivity(intent);
    }

}